package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.RegisterBean;
import com.dinadi.yidi.bean.UserInfoBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.ui.MyApplication;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * author: JiaYu.He
 * Date: 2017/4/26   下一步设置密码
 * Time: 15:59
 */

public class NextSettingActivity extends BaseActivity {
    private View view;
    private ImageView canle_login, is_show;
    private EditText edit_pwd;
    private ImageView btn_login;
    private String phone, code, nextType;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(NextSettingActivity.this).inflate(R.layout.activity_pwd_next, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        canle_login = (ImageView) view.findViewById(R.id.canle_login);
        edit_pwd = (EditText) view.findViewById(R.id.edit_pwd);
        btn_login = (ImageView) view.findViewById(R.id.btn_login);
        is_show = (ImageView) view.findViewById(R.id.is_show);

        edit_pwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0)
                    is_show.setVisibility(View.GONE);
                else
                    is_show.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void initListener() {
        registerClickListener(canle_login, btn_login, is_show);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        code = intent.getStringExtra("code");
        nextType = intent.getStringExtra("nextType");
    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.canle_login:
                NextSettingActivity.this.finish();
                break;
            case R.id.btn_login:
                String pwd = edit_pwd.getText().toString();
                boolean b = checkPass(pwd);

                if (!b) {
                    return;
                } else {

                    if (nextType.equals("0")) {
                        //开始注册  0
                        RegisterBean registerBean = new RegisterBean();
                        registerBean.setPhone(phone);
                        registerBean.setPwd(pwd);
                        registerBean.setValidcode(code);
                        showLoadingDialog(getResources().getString(R.string.dialog_text_register));
                        CommonInterface register = new CommonInterface(NextSettingActivity.this, httpListener2);
                        register.getInterface("10002", JSON.toJSONString(registerBean));
                    } else if (nextType.equals("1")) {
                        //修改  1
                        RegisterBean registerBean = new RegisterBean();
                        registerBean.setPhone(phone);
                        registerBean.setPwd(pwd);
                        registerBean.setValidcode(code);
                        showLoadingDialog(getResources().getString(R.string.dialog_text_register));
                        CommonInterface register = new CommonInterface(NextSettingActivity.this, httpListener2);
                        register.getInterface("10004", JSON.toJSONString(registerBean));
                    }


                }
                break;
            case R.id.is_show:
                pwdShow();
                break;
            default:
                break;
        }
    }

    // 检查密码
    public boolean checkPass(String password) {
        if (TextUtils.isEmpty(password)) {
            toast(R.string.edit_need_pwd);
            return false;
        }

        if (password.length() < 6) {
            toast(R.string.edit_need_pwd6);
            return false;
        }

        boolean isDigit = false;//定义一个boolean值，用来表示是否包含数字
        boolean isLetter = false;//定义一个boolean值，用来表示是否包含字母
        for (int i = 0; i < password.length(); i++) { //循环遍历字符串
            if (Character.isDigit(password.charAt(i))) {     //用char包装类中的判断数字的方法判断每一个字符
                isDigit = true;
            }
            if (Character.isLetter(password.charAt(i))) {   //用char包装类中的判断字母的方法判断每一个字符
                isLetter = true;
            }
        }

        if (isDigit != true || isLetter != true) {
            toast(R.string.edit_limit_pwd);
            return false;
        }
        return true;
    }

    public void pwdShow() {

        int type = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
        if (edit_pwd.getInputType() == type) {
            edit_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            edit_pwd.setSelection(edit_pwd.getText().length()); // 把光标设置到当前文本末尾
            is_show.setImageResource(R.drawable.login_icon_password_display);
        } else {
            edit_pwd.setInputType(type);
            edit_pwd.setSelection(edit_pwd.getText().length());
            is_show.setImageResource(R.drawable.login_icon_password_hide);
        }

    }

    HttpListener httpListener2 = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                toast("登录成功");
                startActivity(new Intent(NextSettingActivity.this, MainActivity.class));
                NextSettingActivity.this.finish();
                MyApplication.getApp().setUser(JsonUtil.parseJsonToBean(response, UserInfoBean.class));
                String token = JsonUtil.getFieldValue(response, "token");
                SharePreferencesHelper.getInstance(NextSettingActivity.this).putString("token", token);
                String userId = JsonUtil.getFieldValue(response, "userId");
                SharePreferencesHelper.getInstance(NextSettingActivity.this).putString("userId", userId);
            } else {
                closeLoadingDialog();
                toast("手机号或密码错误");
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("注册失败");
        }
    };
}
